import { computed } from "vue";
import type { ComputedRef } from "vue";

export default function <T extends (...args: any[]) => any>(callBack: T): (...args: Parameters<T>) => ComputedRef<ReturnType<T>> {
  const cacheData = new Map<string, ComputedRef<ReturnType<T>>>();

  return (...args: Parameters<T>): ComputedRef<ReturnType<T>> => {
    const argsKey = JSON.stringify(args);
    const cachedResult = cacheData.get(argsKey);

    if (cachedResult) {
      return cachedResult;
    } else {
      const result = computed(() => callBack(...args));
      cacheData.set(argsKey, result);
      return result;
    }
  };
}
